package com.database;
import java.sql.*;

import com.Helper.CustomerInfo;

public class CustomerDB extends Database
{
	private String username = null;
	private String password = null;
	private String firstName = null;
	private String lastName = null;
	String email = null;
	private int phone;
	private int loyaltypoint;
	
	public CustomerDB()
	{
		tableName = "Customer";
	}
	//@requires username !== Null;
	//@signals_only RuntimeException, IOException,SQLException;
	public CustomerDB(String username) throws SQLException
	{
		if (UserExists(username))
		{
			System.out.println(tableName + " populated");
		}
		else
		{
			System.out.print(tableName + " " + username + "does not exist!");
		}
	}
	
	//Constructor used to insert data into the database
	//@modifies this
	public CustomerDB(String username, String password, String firstName, String lastName, String email, int phone, int loyaltypoint)
	{
		tableName = "Customer";
		
		this.username = username;
		this.password = password;
		this.firstName = firstName;
		this.lastName = lastName;
		this.email = email;
		this.phone = phone;
		this.loyaltypoint = loyaltypoint;
	}
	
	public String GetFirstName()
	{
		return firstName;
	}
	
	public String GetLastName()
	{
		return lastName;
	}
	
	public String GetEmail()
	{
		return email;
	}
	
	public int GetPhone()
	{
		return phone;
	}
	
	public int GetLoyaltyPoint()
	{
		return loyaltypoint;
	}
	//@requires password !== Null;
    //@assignable Statement,sqlString,ResultSet;
	//@signals_only RuntimeException, IOException,SQLException;
	public boolean UserExists(String username) throws SQLException
	{
		Statement stmt = connection.createStatement();
		String sqlString = "select * from " + tableName + " where username = " + AddQuotes(username);
		
		ResultSet rs = stmt.executeQuery(sqlString);
		
		if (rs.next())//check if user exists in database
		{
			this.username = rs.getString("username");
			this.password = rs.getString("password");
			this.firstName = rs.getString("firstname");
			this.lastName = rs.getString("lastname");
			this.email = rs.getString("email");
			this.phone = Integer.parseInt(rs.getString("phone"));
			this.loyaltypoint = Integer.parseInt((rs.getString("loyaltypoint")));
			return true;
		}
		else
		{
			return false;
		}
	}
	//@requires password !== Null;
    //@assignable CustomerInfo, Statement,sqlString,ResultSet;
	//@signals_only RuntimeException, IOException,SQLException;
	public CustomerInfo GetSelectedCustomer(String username) throws SQLException
	{
		CustomerInfo info = new CustomerInfo();
		Statement stmt = connection.createStatement();
		String sqlString = "select * from " + tableName + " where username = " + AddQuotes(username);
		
		System.out.println(sqlString);
		
		ResultSet rs = stmt.executeQuery(sqlString);
		
		if (rs.next())//get customer info
		{
			info.username = rs.getString("username");
			info.password = rs.getString("password");
			info.firstName = rs.getString("firstname");
			info.lastName = rs.getString("lastname");
			info.email = rs.getString("email");
			info.phone = Integer.parseInt(rs.getString("phone"));
			info.point = Integer.parseInt((rs.getString("loyaltypoint")));
			return info;
		}
		else
		{
			return null;
		}
	}
	//@requires password !== Null;
    //@assignable newPassword,username;
	//@signals_only RuntimeException, IOException,SQLException;
	public void ModPassword(String newPassword, String tupleID) throws SQLException
	{
		super.Modify(newPassword, "password","username",tupleID); 
	}
	//@requires firstname !== Null;
    //@assignable newFirstName, username;
	//@signals_only RuntimeException, IOException,SQLException;
	public void ModFirstName(String newFirstName, String tupleID) throws SQLException
	{
		super.Modify(newFirstName, "firstname","username",tupleID); 
	}
	//@requires lastname!== Null;
    //@assignable newLastName, username;
	//@signals_only RuntimeException, IOException,SQLException;
	public void ModLastName(String newLastName, String tupleID) throws SQLException
	{
		super.Modify(newLastName, "lastname","username",tupleID); 
	}
	//@requires email !== Null;
    //@assignable newEmail,username;
	//@signals_only RuntimeException, IOException,SQLException;
	public void ModEmail(String newEmail, String tupleID) throws SQLException
	{
		super.Modify(newEmail, "email","username",tupleID); 
	}
	//@requires phone !== Null;
    //@assignable newPhone,username;
	//@signals_only RuntimeException, IOException,SQLException;
	public void ModPhone(int newPhone, String tupleID) throws SQLException
	{
		super.Modify(newPhone, "phone","username",tupleID);
	}
	//@requires points!== Null;
    //@assignable newPoints,username;
	//@signals_only RuntimeException, IOException,SQLException;
	public void ModPoints(int newPoints, String tupleID) throws SQLException
	{
		super.Modify(newPoints, "loyaltypoint","username",tupleID);
	}
	
	@Override
    //@assignable statement,sqlString;
	//@signals_only RuntimeException, IOException,SQLException;
	public void Insert() throws SQLException 
	{
		Statement stmt = connection.createStatement();
				
	    String sqlString = "INSERT INTO " + tableName + " VALUES (" +
	    					AddQuotes(username) + ", " +
	    					AddQuotes(password) + ", " +
	    					AddQuotes(firstName) + ", " +
	    					AddQuotes(lastName) + ", " +
	    					AddQuotes(email) + ", " +
	    					123 + ", " +
	    					loyaltypoint + 
	    					")";
	    stmt.executeUpdate(sqlString);
	    System.out.println(sqlString);
	}
	
	@Override
    //@assignable Statement, sqlString;
	//@signals_only RuntimeException, IOException,SQLException;
	public void Delete(String primaryKey) throws SQLException 
	{
		Statement stmt = connection.createStatement();
		
		String sqlString = "DELETE from " + tableName + " WHERE username = " + AddQuotes(primaryKey);
		stmt.executeUpdate(sqlString);
	}

	@Override
    //@assignable sqlString, Statement;
	//@signals_only RuntimeException, IOException,SQLException;
	public void Display() throws SQLException 
	{
	    String sqlString = "SELECT * FROM " + tableName;
	    Statement stmt = connection.createStatement();

	    ResultSet rs = stmt.executeQuery(sqlString);

	    while (rs.next())
	    {
	        System.out.println
	        (
	            rs.getString("username") + " " +
	            rs.getString("password") + " " +
	            rs.getString("firstname") + " " +
	            rs.getString("lastname") + " " +
	            rs.getString("email") + " " +  
	            rs.getString("phone") + " " +  
	            rs.getString("loyaltypoint") + " "
	        );
	    }	
	}
	
	public String GetUsername()
	{
		return username;
	}
	
	public String GetPassword()
	{
		return password;
	}

}
